package upload;

import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;
import java.io.IOException;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: YKH
 * Date: 2022-10-29
 * Time: 13:53
 */
@MultipartConfig //上传文件需要加这个注解 ( 可以认为Servlet上传文件功能是默认关闭的,需要通过这个注解手动打开)
@WebServlet("/upload")
public class UploadServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取文件放到part
        Part part = req.getPart("MyFile");
        //获取文件的真实名
        System.out.println(part.getSubmittedFileName());
        //获取文件大小
        System.out.println(part.getSize());
        //获取文件类型
        System.out.println(part.getContentType());
        //将文件保存到服务器的磁盘中
        part.write("d:/新建文件夹 (2)/result.jpg");
        resp.getWriter().write("upload is ok");
    }
}
